<!-- 客户管理 -->
<template>
    <div>

        <dxtable :headBtn="headBtn" :dataJson="dataJson" :portUrl="portUrl" :formJson="formJson" :qx="qx" :czFun="czFun"
            ref="dxtableref" @getlistId="getlistId" :hidebj="hidebj" :isBfy="true"></dxtable>
        <n-modal v-model:show="showModal" preset="card" class="dsfsdtretretret">
            <template #header>
                <div>入库登记</div>
            </template>
            <div>
                <div class="">
                    客户: {{ dqkh.name }}
                </div>
                <div class="mt10 display-flex ">
                    <span class="vertical-center">
                        拿货数量：
                    </span>
                    <p class="flex-1">
                        <n-input-number v-model:value="QuantityGoodsNumber" min="1" type="text" placeholder="" size="medium"
                            clearable></n-input-number>

                    </p>
                </div>
            </div>
            <template #action>
                <div class="tr">
                    <n-button @click="showModal = false"> 取消 </n-button>
                    <n-button type="primary" size="medium" @click="addNh" class="ml20">确认</n-button>
                </div>
            </template>
        </n-modal>
        <n-modal v-model:show="showModaler" preset="card" class="dsfsdtretretret">
            <template #header>
                <div>拿货记录</div>
            </template>
            <div>
                <div class="display-flex">
                    <p class="flex-1">
                        客户:{{ dqkh.name }}
                    </p>
                    <p class="flex-1 tr">
                        多久没拿货: {{ dqkh.interval }}(天)
                    </p>
                </div>
                <div class="naheerlist mt10">
                    <n-data-table :columns="columns" :data="nahuodata" />
                </div>

            </div>
            <template #action>
                <div class="tr">
                    <n-button @click="showModaler = false"> 关闭 </n-button>
                </div>
            </template>
        </n-modal>
    </div>
</template>
<script lang='ts' setup>
import { leixing } from "../../util/config";
import dxtable from "../../components/util/dxtable.vue";
import { dxget, dxpost, transferTimeYMD, qurl } from "../../util/index";
import { NButton } from "naive-ui"
import { h, ref, reactive } from "vue";
const cz = qurl();
let qxList = ref([]);
const dfsdta = ref([])
const dfsdtb = ref([])
const showModal = ref(false)
const showModaler = ref(false)
const dxtableref = ref()
const nahuodata = ref()
const hidebj = ref()
let headBtn = [];
const QuantityGoodsNumber = ref(1)
const dqkh = ref()
const nhzsl = ref()
const emit = defineEmits(["callBack"]);
const ijjerr = ref({})
const props = defineProps({
    pup: { type: String },
});
const columns = [
    {
        title: '拿货数量',
        key: 'QuantityGoodsNumber'
    },
    {
        title: '拿货时间',
        key: 'addTime'
    },
]
let pupVal = cz.pup || props.pup;

let dataJson = [
    {
        title: "姓名",
        key: "name",
        sorter: 'default',
        search: true, // 是否在搜索栏展示
    },
    {
        title: "地址",
        key: "add",
        search: true, // 是否在搜索栏展示
    },

    {
        title: "电话",
        key: "phone",
        search: true, // 是否在搜索栏展示
    },
    {
        title: "录入时间",
        key: "addTime",
        sorter: 'default',
    },

    {
        title: "客户级别",
        key: "CustomerLevel",
        sorter: 'default',
        render(row) {
            try {
                if(!row.share){
                    row.share = []
                }else{
                    const jher = []
                row.share.split(",").map(a => {
                    jher.push(parseInt(a))
                })
                row.share = jher
                }
             

            } catch (e) {
             
            
            }
            return dfsdta.value.filter(a => a.value == row.CustomerLevel)[0].label
        }
    },
    {
        title: "上次拿货数量",
        key: "QuantityGoodsReceived",
        sorter: 'default',
        width: 120,
        render(row) {
            return row.QuantityGoodsReceived || '--'
        }
    },

    {
        title: "拿货记录(次)",
        width: 120,
        key: "TakeUpTimes",
        sorter: 'default'
    }, {
        title: "上次拿货时间",
        key: "DateDelivery",
        width: 150,
        sorter: 'default',
        render(row) {
            return row.DateDelivery || '--'
        }
    },
    {
        title: "多久没拿货(天)",
        width: 120,
        sorter: 'default',
        key: "DateDelivery",
        render(row) {
            if (row.DateDelivery) {
                const timea = new Date(row.DateDelivery).getTime()
                const timeb = new Date().getTime()
                const jsde = parseInt((timeb - timea) / (1000 * 60 * 60 * 24))
                return jsde || '--'
            }
            return '--'
        }
    },


];
let qx = {
    DeletePermissionID: "sacustomerManagementdel", //删除 
    EditPermissionIdentifier: "sacustomerManagementbj", // 编辑
    NewPermissionIdentifier: "sacustomerManagementxz", // 新建
    ViewPermissionID: "sacustomerManagementck", // 查看
};
// const portUrl = "dxgjAdmin/administrator?CharacterNameCode=" + leixing;
let portUrl = "salesManagement/xstradeDdebtor"
if (localStorage.id == 42) {
    portUrl = "salesManagement/xstradeDdebtor?salesman=" + localStorage.Userid;
}
if (pupVal) {
    portUrl = "salesManagement/getnhnumer/getoverdue?salesman=" + localStorage.Userid;;
    hidebj.value = true
    dataJson = [
        {
            title: "姓名",
            key: "name",
            search: true, // 是否在搜索栏展示
        },
        {
            title: "地址",
            key: "add",
        },

        {
            title: "电话",
            key: "phone",
        },
        {
            title: "录入时间",
            key: "addTime",
        },

        {
            title: "客户级别",
            key: "CustomerLevel",
            render(row) {
                try {
                    const jher = []
                    row.share.split(",").map(a => {
                        jher.push(parseInt(a))
                    })
                    row.share = jher
                    console.log(row.share);
                } catch (e) {

                }
                return dfsdta.value.filter(a => a.value == row.CustomerLevel)[0].label
            }
        },
    ];

    headBtn = [
        {
            name: "确认选择 ",
            async clickFun(row) {
                const arrList = row.join(",");
                const res = await dxpost("salesManagement/getnhnumer/xgywy", {
                    arrList: arrList,
                    salesman: localStorage.Userid
                });
                emit("callBack");
            },
        },
    ];

    qx = {
        DeletePermissionID: "sacustomerManagementdelaa", //删除 
        EditPermissionIdentifier: "sacustomerManagementbjaa", // 编辑
        NewPermissionIdentifier: "sacustomerManagementxzaa", // 新建
        ViewPermissionID: "sacustomerManagementck", // 查看
    };
}
if (localStorage.id == 41) {
    dataJson.splice(4, 0, {
        title: "可维护业务员",
        key: "salesman",
        sorter: 'default',
        render(row) {
            try {
                return dfsdtb.value.filter(a => a.value == row.salesman)[0].label
            } catch (e) {

            }
        }
    })

}
let formJson = reactive([
    {
        title: "姓名",
        key: "name",
        type: "input",
        required: true, // 是否为必填
    },
    {
        title: "地址",
        key: "add",
        type: "input",
        required: true, // 是否为必填
    },
    {
        title: "电话",
        key: "phone",
        type: "input",
        required: true, // 是否为必填
    },
    {
        title: "客户级别",
        key: "CustomerLevel",
        type: "select",
        options: [],
        val: "1"
    },
]);
if (localStorage.id == 41) {
    formJson.push({
        title: "可维护业务员",
        key: "salesman",
        type: "select",
        sorter: 'default',
        options: [],
        required: true, // 是否为必填
    })
    formJson.push({
        title: "授权给其他业务员",
        key: "share",
        options: [],
        type: "select",
        multiple: true,
        span: 24
    })
} else {
    formJson.push({
        title: "授权给其他业务员",
        key: "share",
        options: [],
        type: "select",
        multiple: true,
        span: 24
    })
    formJson.push({
        title: "",
        key: "salesman",
        val: localStorage.Userid
    })

}

const getzd = async () => {
    const { data }: any = await dxget("dxgjAdmin/getzd", {
        name: "xsdj"
    })
    formJson[3].options = data
    dfsdta.value = [{
        label: "全部",
        value: "",
    },
    ...data
    ]
}
getzd()
const getywy = async () => {
    const { data }: any = await dxget("salesManagement/getywy", {
        CharacterNameCode: 42
    })
    if (data) {
        const jjhertr = []
        data.map(a => {
            jjhertr.push({
                label: a.userNickname,
                value: a.id
            })
        })
        formJson[4].options = jjhertr
        try {
            formJson[5].options = jjhertr
        } catch (e) {

        }
        dfsdtb.value = [{
            label: "全部",
            value: "",
        },
        ...jjhertr
        ]
    }
}
getywy()
const czFun = {
    fun: (row) => {
        return [
            h(
                NButton,
                {
                    type: "info",
                    quaternary: true,
                    onClick: async (e) => {
                        showModal.value = true
                        dqkh.value = row
                    },
                },
                "入库登记"
            ),
        ];
    },
};
const addNh = async () => {
    const { errno }: any = await dxpost("salesManagement/xsWarehousingRegistration", {
        clientId: dqkh.value.id,
        QuantityGoodsNumber: QuantityGoodsNumber.value
    })
    if (errno == 0) {
        window.$message.success("操作成功")
        showModal.value = false
        QuantityGoodsNumber.value = 1
        dxtableref.value.initDataExpose()
    }
}

const getlistId = async (tb) => {
    if (!tb) {
        return
    }
    const { data }: any = await dxget("salesManagement/getnhnumer", {
        clientId: tb
    })
    nhzsl.value = data
}

defineExpose({
    initDataExpose() {
        dxtableref.value.initDataExpose()
    }
})
</script>
<style scoped>
.naheerlist {
    max-height: 600px;
    overflow: auto;
}
</style>